package com.douyu.data.rabbitmq.exchange;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class CreateExchangeAndQueue {

        public static void main(String[] args) {
//        1. 创建连接工程
            ConnectionFactory connectionFactory = new ConnectionFactory();
            connectionFactory.setHost("172.22.34.1"); // 172.22.34.1
            connectionFactory.setPort(5672);
            connectionFactory.setUsername("admin");
            connectionFactory.setPassword("admin");
            connectionFactory.setVirtualHost("healerVH");
            Connection connection;
            Channel channel;
            try {
//            2. 创建连接Connection
                connection = connectionFactory.newConnection("生产者");
                //        3. 通过连接获取通道Channel
                channel = connection.createChannel();

                //        4. 通过创建交换机，声明队列，绑定关系，路由key,发送消息，和接收消息
                String queueName = "bilibili";

                /**
                 *
                 * @param queue - 队列名
                 * @param durable - 是否要持久化，
                 * @param exclusive - 排他性，是否独占队列
                 * @param autoDelete - 是否自动删除，随着最后一个消费者消息完毕消息后，是否把队列自动删除
                 * @param arguments
                 * @throws IOException
                 */
                channel.queueDeclare(queueName,true,false,false,null);
//        5. 准备消息内容
                String message = "chenp要睡觉了";
//        6. 发送消息给队列queue
                String exchangeName = "healer.direct";
                String type = "direct";
                channel.exchangeDeclare(exchangeName, type);
                channel.queueBind(queueName, exchangeName, "joy");
//        7. 关闭通道
                if(channel != null && channel.isOpen()){
                    channel.close();
                }
//        8. 关闭连接
                if(connection != null && connection.isOpen()){
                    connection.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (TimeoutException e) {
                e.printStackTrace();
            }
        }


    }
